package org.px.gui;

import org.px.domain.UserIdentity;
import org.px.service.UserDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class LoginFrame extends JFrame {
    private JTextField userIdField;
    private JPasswordField passwordField;
    private UserDao userDao = new UserDao();
    
    public LoginFrame() {
        initUI();
    }
    
    private void initUI() {
        setTitle("员工请假管理系统 - 登录");
        setSize(800,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        
        // 用户ID标签和输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("用户ID:"), gbc);
        
        gbc.gridx = 1;
        userIdField = new JTextField(25);
        panel.add(userIdField, gbc);
        
        // 密码标签和输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("密码:"), gbc);
        
        gbc.gridx = 1;
        passwordField = new JPasswordField(25);
        panel.add(passwordField, gbc);
        
        // 登录按钮
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        JButton loginBtn = new JButton("登录");
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doLogin();
            }
        });
        loginBtn.setPreferredSize(new Dimension(120, 40));
        panel.add(loginBtn, gbc);
        
        // 注册按钮
        gbc.gridy = 3;
        JButton registerBtn = new JButton("注册新账号");
        registerBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openRegisterFrame();
            }
        });
        registerBtn.setPreferredSize(new Dimension(150, 40));
        panel.add(registerBtn, gbc);
        
        add(panel);
    }
    
    private void doLogin() {
        String userId = userIdField.getText().trim();
        String password = new String(passwordField.getPassword());
        
        if (userId.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户ID和密码不能为空", "登录错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        try {
            UserIdentity user = userDao.findByUserIdAndPassword(userId, password);
            if (user != null) {
                System.out.println("用户类型：" + user.getUser_type());
                System.out.println("用户ID：" + user.getUser_id());
                System.out.println("用户名：" + user.getUsername());
                System.out.println("密码：" + user.getPassword());
                System.out.println("状态：" + user.getStatus());
                JOptionPane.showMessageDialog(this, "登录成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                // 登录成功，打开主界面
                openMainFrame(user);
                //dispose(); // 关闭登录界面
            } else {
                JOptionPane.showMessageDialog(this, "用户ID或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库操作异常：" + ex.getMessage(), "系统错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void openRegisterFrame() {
        RegisterFrame registerFrame = new RegisterFrame(this);
        registerFrame.setVisible(true);
    }
    
    private void openMainFrame(UserIdentity user) {
        // 根据用户类型打开不同的主界面
        if ("admin".equalsIgnoreCase(user.getUser_type())) {
            new AdminMainFrame(user).setVisible(true);
        } else {
            new EmployeeMainFrame(user).setVisible(true);
        }
    }

    
    
}